package com.mystnihon.freeremote.ui.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.webkit.URLUtil;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.actionbarsherlock.app.SherlockFragment;
import com.mystnihon.freeremote.R;
import com.mystnihon.freeremote.ui.UploadActivity;

public class UrlChooserFragment extends SherlockFragment implements OnClickListener, OnEditorActionListener,
		TextWatcher {

	private EditText mEditUrlZone;
	private Button mButtonUpload;

	public static UrlChooserFragment newInstance() {
	    UrlChooserFragment f = new UrlChooserFragment();
        return f;

    }
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		final View v = inflater.inflate(R.layout.fragment_upload_url, null);
		mEditUrlZone = (EditText) v.findViewById(R.id.fr_add_torrent_url_edittext);
		mEditUrlZone.setOnEditorActionListener(this);
		mEditUrlZone.addTextChangedListener(this);
		mButtonUpload = (Button) v.findViewById(R.id.fr_add_torrent_url_btn);
		mButtonUpload.setOnClickListener(this);
		return v;
	}

	@Override
	public void onClick(View view) {
		String uriString = mEditUrlZone.getEditableText().toString();
		proceed(uriString);
	}

	@Override
	public void afterTextChanged(Editable s) {
		if (URLUtil.isValidUrl(s.toString())) {
			mButtonUpload.setEnabled(true);
		} else {
			mButtonUpload.setEnabled(false);
		}

	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count, int after) {

	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {

	}

	@Override
	public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
		switch (actionId) {

			case EditorInfo.IME_ACTION_GO:
				String uriString = v.getText().toString();
				proceed(uriString);

				return true;

		}
		return false;
	}

	private boolean proceed(String uriString) {
		if (URLUtil.isValidUrl(uriString)) {
			Uri uri = Uri.parse(uriString);
			closeImeKeyboard();
			getSherlockActivity().setResult(Activity.RESULT_OK,
					new Intent().putExtra(UploadActivity.EXTRA_RESPONSE_URI, uri));
			getSherlockActivity().finish();
			return true;
		} else {
			return false;
		}
	}

	private void closeImeKeyboard() {
		InputMethodManager ime = (InputMethodManager) getSherlockActivity().getSystemService(
				Context.INPUT_METHOD_SERVICE);
		ime.hideSoftInputFromWindow(mEditUrlZone.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
	}

}
